package com.example.blog.controller;

import com.example.blog.constant.Constants;
import com.example.blog.model.Result;
import com.example.blog.model.UserInfo;
import com.example.blog.service.UserService;
import com.example.blog.utils.JwtUtils;
import com.example.blog.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Map;

@RequestMapping("/user")
@RestController
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/register")
    public Result register(String username, String password) {
        log.info("用户注册: userName:" + username + " , password:" + password);
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            log.error("账号或密码不能为空");
            return Result.fail("账号或密码不能为空!");
        }
        //从数据库中查找用户, 判断用户是否已经存在
        UserInfo userInfo = userService.selectByName(username);
        if(userInfo != null) {
            log.error("用户已被注册");
            return Result.fail("用户已经注册了");
        }
        //在数据库中插入一条用户数据
        String sqlPassword = SecurityUtils.encrypt(password);
        userInfo = new UserInfo();
        userInfo.setUserName(username);
        //数据库中存储加密后的密码
        userInfo.setPassword(sqlPassword);
        Boolean res = userService.insertUserInfo(userInfo);
        if(res) {
            return Result.success(null);
        }
        return Result.fail("内部错误, 注册失败!");
    }

    @RequestMapping("/login")
    public Result login(String username, String password) {
        log.info("用户登录: userName:" + username + " , password:" + password);
        //1.参数校验
        //2.校验密码是否正确
        //3.密码正确, 返回token
        //4.密码错误, 返回错误信息
        if(!StringUtils.hasLength(username) || !StringUtils.hasLength(password)) {
            log.error("账号或密码不能为空");
            return Result.fail("账号或密码不能为空!");
        }
        //从数据库中查找用户
        UserInfo userInfo = userService.selectByName(username);
        if(userInfo == null) {
            log.error("用户不存在");
            return Result.fail("用户不存在");
        }
        if(!SecurityUtils.verify(password, userInfo.getPassword())) {
            log.error("密码错误");
            return Result.fail("密码错误!");
        }
        //密码正确, 返回token
        Map<String, Object> claim = new HashMap<>();
        claim.put(Constants.TOKEN_ID,userInfo.getId());
        claim.put(Constants.TOKEN_USERNAME, userInfo.getUserName());
        String token = JwtUtils.genJwtToken(claim);
        log.info("用户登录成功, token:{}", token);
        return Result.success(token);
    }

    /**
     * 获取当前登录用户的信息
     * @return
     */
    @RequestMapping("/getUserInfo")
    public UserInfo getLoginUserInfo(HttpServletRequest request) {
        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //从 token 中获取登录用户 id
        Integer userId = JwtUtils.getIdByToken(token);
        if(userId == null) {
            //用户未登录
            return null;
        }
        UserInfo userInfo = userService.selectById(userId);
        return userInfo;
    }

    @RequestMapping("/getAuthorInfo")
    public UserInfo getAuthorInfo(Integer blogId) {

        if(blogId <= 0) {
            return null;
        }

        UserInfo userInfo = userService.getAuthorInfo(blogId);
        return userInfo;
    }
}
